package edu.group2.jira.plugin;

import com.atlassian.jira.web.action.JiraWebActionSupport;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.sql.Connection;
import org.ofbiz.core.entity.ConnectionFactory;

import com.opensymphony.user.UserManager;
import com.opensymphony.user.provider.*;




public class FilterChown extends JiraWebActionSupport {
	
	public FilterChown() {
		
	}

	protected void doValidation() {
		String s = getFilterId();

        if (s == null) {
            addErrorMessage("The local variable didn't get set");
            return;
        }

        if (s.indexOf("bob") != -1) {
        	log.debug("An error message has been set");
        }

        if (invalidInput()) {
            for (Iterator it = getErrorMessages().iterator(); it.hasNext();) {
                String msg = (String)it.next();
            }

            for (Iterator it2 = getErrors().entrySet().iterator(); it2.hasNext();) {
                Map.Entry entry = (Map.Entry)it2.next();
                log.debug("Error during validation: field=" + entry.getKey() + ", error=" + entry.getValue());
            }
        }
    }

    protected String doExecute() throws Exception {
    	    	
    	filterIdVariable = request.getParameter("filterId");
    	usernameVariable = request.getParameter("username");
        
        Connection conn = null;
        
        try {
        	conn = ConnectionFactory.getConnection("defaultDS");
        } catch (SQLException e1) {
        	System.out.println(e1.getMessage());
        }
        
       try {
    	   Statement stmt = conn.createStatement();
    	   
    	   String sql = "UPDATE searchrequest SET authorname=" +
    	   		"'" + usernameVariable + "', username='" + usernameVariable + "' WHERE id = '" + filterIdVariable + "'";
    	   
    	   System.out.println(sql);
    	   
    	   stmt.execute(sql);
    	   UserManager.getInstance().flushCaches();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return SUCCESS;
    }


    private String filterIdVariable = "";
    private String usernameVariable = "";

    public void setFilterId(String value) {
        log.debug("Setting filterIdVariable to: " + value);
        this.filterIdVariable = value;
    }

    public String getFilterId() {
        log.debug("Getting aStringVariable");
        return filterIdVariable;
    }
    
    public void setUsername(String value) {
        log.debug("Setting usernameVariable to: " + value);
        this.usernameVariable = value;
    }

    public String getUsername() {
        log.debug("Getting usernameVariable");
        return usernameVariable;
    }
}